export default class cPlayer {
	_player;
	_id;
	_url_ws;
	_h;
	constructor(domId) {
		this._id = domId;
		this.hInit({
			id: domId
		});
	}
	hInit({
		id,
		split
	}) {
		this._id = id;

		clearTimeout(this._h);
		if (!window.JSPlugin) {
			this._h = setTimeout(() => {
				this.hInit({
					id: id,
					split: split
				});
			}, 500)
			return;
		}

		if (!id) {
			let dom = document.createElement("div"),
				win = document.createElement("div"),
				btn = document.createElement("button");

			dom.setAttribute("style", `
				    position: absolute;
				    width: 900px;
				    height: 600px;
				    transform: translate(-50%,-50%);
				    z-index: 9999;
				    background-color: #222;
				    top: 50%;
				    left: 50%;
			`);


			let id = new Date().getTime() + Math.random().toString(32).slice(2, 10);
			win.setAttribute("id", id);
			win.setAttribute("style", `width:100%;height:100%`);

			btn.innerText = "关闭";
			btn.addEventListener("click", (e) => {
				dom.style.display = "none";
			});

			dom.appendChild(win);
			dom.appendChild(btn);
			document.body.appendChild(dom);
			this._id = id;
		}


		var _player = new window.JSPlugin({
			szId: id,
			szBasePath: "./lib/hk",
			iMaxSplit: 2,
			iCurrentSplit: 2,
			//openDebug: true,
			oStyle: {
				borderSelect: '#FFCC00',
			}
		});

		this._player = _player;
		_player.JS_Resize();

		// 事件回调绑定
		_player.JS_SetWindowControlCallback({
			windowEventSelect: function(iWndIndex) { //插件选中窗口回调
				//console.log('选中窗口: ', iWndIndex);
			},
			pluginErrorHandler: function(iWndIndex, iErrorCode, oError) { //插件错误回调
				console.log('插件错误: ', iWndIndex, iErrorCode, oError);
			},
			windowEventOver: function(iWndIndex) { //鼠标移过回调
				//console.log(iWndIndex);
			},
			windowEventOut: function(iWndIndex) { //鼠标移出回调
				//console.log(iWndIndex);
			},
			windowEventUp: function(iWndIndex) { //鼠标mouseup事件回调
				//console.log(iWndIndex);
			},
			windowFullCcreenChange: function(bFull) { //全屏切换回调
				console.log('全屏切换回调: ', bFull);
			},
			firstFrameDisplay: function(iWndIndex, iWidth, iHeight) { //首帧显示回调
				console.log('首帧显示回调: ', iWndIndex, iWidth, iHeight);
			},
			performanceLack: function() { //性能不足回调
				console.log('性能不足回调: ');
			}
		});

		this.hSetSplit()

		window.addEventListener('resize', () => {
			this._player?.JS_Resize();
		});
	}
	hSetSplit(split) {
		//split  1:1x1,2:2x2,3:3x3
		this._player.JS_ArrangeWindow(split || 1).then(
			(r) => {
				this._player.JS_Resize();
				console.log(`完成 arrangeWindow to ${split}x${split} success`, r);

			},
			e => {
				console.error(e)
			}
		);

	}
	wix = 0;
	hPlay(url) {
		//预览'ws://10.19.147.22:559/EUrl/q2jQie4';
		this._url_ws = url;
		//url = url.replaceAll("ws://", "wss://");
		let index = this._player.currentWindowIndex || 0;
		console.log("预览", url, this._player.currentWindowIndex);

		if (Array.isArray(url)) {

			for (var i = 0; i < url.length; i++) {
				let ws = url[i];
				this.#play(ws, i);
			}
			return;
		}
		this.#play(url, index);
	}
	#play(ws, ix) {
		this._player?.JS_Play(ws, {
			playURL: ws,
			mode: 1 //0：普通模式，1:高级模式
		}, ix).then(
			() => {
				console.log('预览 成功');
				this._player.JS_Resize();
			},
			e => {
				console.error("预览 异常", e);
			}
		);
	}
	hStop() {
		//JS_StopRealPlayAll
		this._player.JS_Stop().then(
			() => {
				console.log('预览 停止成功')
			},
			e => {
				console.error(e)
			}
		);
	}
	hFullScreen() {
		this._player.JS_FullScreenDisplay(true).then(
			() => {
				console.log(`wholeFullScreen success`)
			},
			e => {
				console.error(e)
			}
		)
	}
}